package com.gzbugu.bugusys.action;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;

import com.gzbugu.bugusys.dao.IBaseDAO;
import com.gzbugu.bugusys.vo.Reimburse;
import com.gzbugu.bugusys.vo.Staff;
import com.gzbugu.bugusys.vo.Type;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 添加报销信息action
 * 
 * @author gauze
 * 
 */
@Controller
public class AddReimburseAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 3284733337650758361L;
	private Reimburse reimburse;
	private String staffId;// 员工编号
	private Short typeId;// 类型id
	private Short handId;// 是否需要上交发票
	private Short statusId;// 报销状态

	@Resource(name = "baseDAO")
	private IBaseDAO baseDAO;

	@Transactional
	public String execute() {
		reimburse.setTypeByTypeId((Type) baseDAO.findById(typeId,
				com.gzbugu.bugusys.fields.VoClasseFields.TYPE));// 设置类型
		reimburse.setTypeByHandIn((Type) baseDAO.findById(handId,
				com.gzbugu.bugusys.fields.VoClasseFields.TYPE));// 设置是否需要上交发票
		reimburse.setTypeByStatus((Type) baseDAO.findById(statusId,
				com.gzbugu.bugusys.fields.VoClasseFields.TYPE));// 设置报销状态
		reimburse.setNotpaid(reimburse.getPayable() - reimburse.getPaid());// 计算并设置欠交发票金额
		baseDAO.save(reimburse);
		return SUCCESS;
	}

	public void validate() {
		if (staffId == null || staffId.trim().length() == 0)
			this.addFieldError("staffIdError", "请输入员工编号");
		else {
			Staff staff = (Staff) baseDAO.findById(staffId,com.gzbugu.bugusys.fields.VoClasseFields.STAFF);
			if (staff == null)
				this.addFieldError("staffIdError", "员工编号不存在");
			else
				this.getReimburse().setStaff(staff);
		}
		if (typeId == null)
			this.addFieldError("typeError", "请选择报销类型");
		if (handId == null)
			this.addFieldError("handError", "请选择是否需要上交发票");
		if (statusId == null)
			this.addFieldError("statusError", "请选择状态");
		if (reimburse != null) {
			if (reimburse.getAmount() == null)
				this.addFieldError("amountError", "请输入金额");
			else if (reimburse.getAmount() < 0)
				this.addFieldError("amountError", "请输入正确的金额");
			if (statusId != null && statusId == 1) {
				if (reimburse.getPayable() == null)
					this.addFieldError("payableError", "请输入金额");
				else if (reimburse.getPayable() < 0)
					this.addFieldError("payableError", "请输入正确的金额");
				if (reimburse.getPaid() == null)
					this.addFieldError("paidError", "请输入金额");
				else if (reimburse.getPaid() < 0
						|| reimburse.getPaid() > reimburse
								.getPayable())
					this.addFieldError("paidError", "请输入正确的金额");
			}
			if (reimburse.getStart() == null)
				this.addFieldError("startError", "请输入开始时间");
			if (reimburse.getEnd() == null)
				this.addFieldError("endError", "请输入结束时间");
			else if (reimburse.getStart() != null
					&& reimburse.getEnd().compareTo(
							reimburse.getStart()) <= 0)
				this.addFieldError("dateError", "请输入正确的开始结束时间");
		} else {
			this.addFieldError("amountError", "请输入金额");
			if (statusId != null && statusId == 1) {
				this.addFieldError("payableError", "请输入金额");
				this.addFieldError("paidError", "请输入金额");
			}
			this.addFieldError("startError", "请输入开始时间");
			this.addFieldError("endError", "请输入结束时间");
		}
	}

	public void setReimburse(Reimburse reimburse) {
		this.reimburse = reimburse;
	}

	public Reimburse getReimburse() {
		return reimburse;
	}

	public String getStaffId() {
		return staffId;
	}

	public void setStaffId(String staffId) {
		this.staffId = staffId;
	}

	public Short getTypeId() {
		return typeId;
	}

	public void setTypeId(Short typeId) {
		this.typeId = typeId;
	}

	public Short getHandId() {
		return handId;
	}

	public void setHandId(Short handId) {
		this.handId = handId;
	}

	public Short getStatusId() {
		return statusId;
	}

	public void setStatusId(Short statusId) {
		this.statusId = statusId;
	}

}
